//
//  SmTableViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "SmTableViewCell.h"
#import "fujinMore.h"
#import "FuJinLastPicCollectionViewCell.h"
@interface SmTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSArray *arr;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, retain) UIView *backView;
@end

@implementation SmTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubViews];
       
       [self createCollection];
       
    }
    return self;
}

- (void)createSubViews{
    
    self.headerImage = [[UIImageView alloc] init];
    [self.contentView addSubview:_headerImage];
    [_headerImage release];
    
    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_nameLabel];
    [_nameLabel release];
    
    self.levelLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_levelLabel];
    [_levelLabel release];
    
    self.timeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_timeLabel];
    [_timeLabel release];
    
    self.tectLabel1 = [[UILabel alloc] init];
    [self.contentView addSubview:_tectLabel1];
    [_tectLabel1 release];
    
    self.backView = [[UIView alloc] init];
    [self.contentView addSubview:_backView];
    [_backView release];
    
    
}

- (void)setModel:(fujinMore *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    self.arr = _model.waypoints;
    [self fuzhi];
    [_collection reloadData];
}

- (void)fuzhi{
    
    NSDictionary *trip = _model.trip;
    NSDictionary *user = trip[@"user"];
    NSString *imageStr = user[@"avatar_m"];
    
    [_headerImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
    
    NSDictionary *experience = user[@"experience"];
    NSDictionary *level_info = experience[@"level_info"];
    
    NSString *str1 = level_info[@"value"];
    _nameLabel.text = [NSString stringWithFormat:@"%@  lv%@", user[@"name"], str1];
    
    _timeLabel.text = trip[@"datetime"];
    _levelLabel.text = trip[@"name"];
    _tectLabel1.text = _model.trip_text;
    
}

- (void)layoutSubviews{
    
    _headerImage.frame = CGRectMake(20, 10, 50, 50);
    _headerImage.layer.cornerRadius = 25;
    _headerImage.layer.masksToBounds = YES;
    _nameLabel.frame = CGRectMake(80, 15, 200, 30);
    _nameLabel.font = [UIFont systemFontOfSize:15];
    
    _levelLabel.frame = CGRectMake(80, 34, 200, 30);
    _levelLabel.font = [UIFont systemFontOfSize:13];
    _levelLabel.textColor = [UIColor blueColor];
    _timeLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 15, 100, 30);
    _timeLabel.font = [UIFont systemFontOfSize:14];
    
    NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    _tectLabel1.numberOfLines = 100;
    
    CGRect rect = [_tectLabel1.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
    
    _tectLabel1.frame = CGRectMake(10, 60, [UIScreen mainScreen].bounds.size.width - 20, rect.size.height);
    
    
    _backView.frame = CGRectMake(0, rect.size.height + 60 + 10, [UIScreen mainScreen].bounds.size.width, 130);
    _backView.backgroundColor = [UIColor redColor];
    
    
    
    
}



- (void)createCollection{
    
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
        self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 130) collectionViewLayout:layout];
        _collection.delegate = self;
        _collection.dataSource = self;
        
    _collection.backgroundColor = [UIColor whiteColor];
        [_backView addSubview:_collection];
        [_collection release];
        
        [_collection registerClass:[FuJinLastPicCollectionViewCell class] forCellWithReuseIdentifier:@"FuJinLastPicCollectionViewCell"];
        
        
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = _model.waypoints;
    NSDictionary *dic = arr[indexPath.row];
    
    
    if ([dic[@"photo_info"] isEqual:[NSNull null]]) {
        return CGSizeZero;
    }else{
        //获取图片的真实高度
        NSDictionary *ph_info = (NSDictionary *)dic[@"photo_info"];
        CGFloat height = [ph_info[@"h"] floatValue];
        CGFloat width = [ph_info[@"w"] floatValue];
        //把图片的高度固定为130
        CGFloat scale = height / 130;
        //图片的宽度为
        CGFloat realWidth = width / scale;
        return CGSizeMake(realWidth, 130);
        
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FuJinLastPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FuJinLastPicCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSDictionary *dic = _arr[indexPath.row];
    
    cell.dic = dic;
    
    return cell;
}

- (void)dealloc{
    [super dealloc];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
